package api

import (
	"api-getwary/basic/globar"
	"api-getwary/handler/request"
	"api-getwary/until"
	"github.com/gin-gonic/gin"
	"net/http"
	__ "api-getwary/basic/proto"
	"strconv"
)

func Videoadd(c *gin.Context) {
	var req request.VideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	res := until.Chect(req.Title)
	if res == false {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "",
			"data":    false,
		})
		return
	}
	videoadd, err := globar.Client.Videoadd(c, &__.VideoaddReq{
		UserId: int64(req.UserId),
		Title:  req.Title,
		Num:    int64(req.Num),
		Info:   req.Info,
		Types:  int64(req.Types),
		Status: int64(req.Status),
		Image:  req.Image,
		VideId: int64(req.VideoID),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"Message": "添加成功",
			"data":    true,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "成功",
		"data":    videoadd,
	})
	return
}

func VideoUpdate(c *gin.Context) {
	var req request.VideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	videoUpdate, err := globar.Client.VideoUpdate(c, &__.VideoUpdateReq{
		Id:     int64(req.Id),
		Info:   req.Info,
		Status: int64(req.Status),
	})
	if err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "修改失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "成功",
		"data":    videoUpdate,
	})
	return
}

func VideoStatus(c *gin.Context) {
	var req request.VideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	videoStatus, err := globar.Client.VideoStatus(c, &__.VideoStatusReq{
		Id: strconv.FormatInt(int64(req.Id), 10),
	})
	if err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "查看失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "成功",
		"data":    videoStatus,
	})
	return
}

func VideoList(c *gin.Context) {
	var req request.VideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	if req.UserId == 0 {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "您不是会员不能查看",
			"data":    false,
		})
		return
	}
	var videoList, err = globar.Client.VideoList(c, &__.VideoListReq{})
	if err != nil {
		c.JSON(http.StatusCreated, gin.H{
			"code":    http.StatusCreated,
			"Message": "查询失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "成功",
		"data":    videoList,
	})
	return
}
